package com.sitech.util.spring;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import javax.servlet.ServletContext;
import javax.sql.DataSource;

import org.springframework.core.io.AbstractResource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

import com.sitech.BaseConst;
import com.sitech.ibnms.util.DesEncrypterIsmp;

import freemarker.cache.ClassTemplateLoader;
import freemarker.cache.TemplateLoader;
import freemarker.ext.beans.BeansWrapper;
import freemarker.template.Configuration;
import freemarker.template.Template;

public class JdbcResource extends AbstractResource
{
	private String content;
	private List<Map> clientBeanList;
	private String clientBean_sql = "SELECT BEANID,SERVICECLASS, ADDRESS FROM T_SYSTEM_WEBSERVICECLIENT";
	private ServletContext servletContext;
	private String jdbcUrl ="";
	public InputStream getInputStream() throws IOException
	{
		return new ByteArrayInputStream(content.getBytes());
	}
	
	
	public JdbcResource() throws Exception
	{
		
		this.servletContext = servletContext;
		getWebserviceClientBean();
		getJaxwsFile();
		System.out.println("JdbcResource Loaded! ");
	}

	public String getDescription()
	{
		return "Basd jdbc Resource";
	}
	
	private Template getJaxwsTemplate() throws Exception
	{
		Configuration cfg = new Configuration();
		
		TemplateLoader templateLoader=new ClassTemplateLoader(this.getClass());  
		
        cfg.setTemplateLoader(templateLoader);
        cfg.setObjectWrapper(new BeansWrapper());
        Template template =  cfg.getTemplate("jaxws.ftl");
        return template;
	}
	
	private void getJaxwsFile() throws Exception
	{
		Template template = getJaxwsTemplate();
		StringWriter writer = new StringWriter();
		
		Map<String, Object> root = new HashMap<String, Object>();
		root.put("beanList", clientBeanList);		
        template.process(root, writer);
        content = writer.toString();
	}
	
	
	private void getWebserviceClientBean() throws Exception
	{
		clientBeanList = new ArrayList<Map>();
				
		String jdbcPath = BaseConst.WEBROOT_PATH + File.separator+"WEB-INF"+File.separator+"common"+File.separator+"conf"+File.separator+"jdbc.properties";
		
		Properties properties = new Properties();
		properties.load(new FileInputStream(new File(jdbcPath)));
		
		DesEncrypterIsmp encrypter = new DesEncrypterIsmp();
		
		System.out.println(properties.getProperty("jdbc.password")+":++++++++++++++++++++++++++++++++++++++++++++");
		String pwd = encrypter.decrypt(properties.getProperty("jdbc.password"));
		System.out.println(pwd+":++++++++++++++++++++++++++++++++++++++++++++");
		pwd = "bnms13";
		
		DataSource dataSource = new DriverManagerDataSource(properties.getProperty("jdbc.driverClassName"),properties.getProperty("jdbc.url"), properties.getProperty("jdbc.username"), pwd);
		jdbcUrl = dataSource.toString();
		JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
		
		jdbcTemplate.query(clientBean_sql, new RowCallbackHandler() {
			public void processRow(ResultSet rs) throws SQLException {
				String beanId = rs.getString(1);
				String serviceClass = rs.getString(2);
				String address = rs.getString(3);
				
				Map map = new HashMap<String, String>();
				map.put("beanId", beanId);
				map.put("serviceClass", serviceClass);
				map.put("address", address);
				
/*				WebServiceClientBean webServiceClientBean = new WebServiceClientBean();
				webServiceClientBean.beanId = beanId;
				webServiceClientBean.serviceClass = serviceClass;
				webServiceClientBean.address = address;*/
				clientBeanList.add(map);
			}
		});
	}
	
	/**
	 * This implementation always throws IllegalStateException,
	 * assuming that the resource does not have a filename.
	 */
	public String getFilename() throws IllegalStateException {
		return "spring bean from" +jdbcUrl;
	}
	
	class  WebServiceClientBean
	{
		private String beanId;
		private String serviceClass;
		private String address;
		public String getBeanId()
		{
			return beanId;
		}
		public void setBeanId(String beanId)
		{
			this.beanId = beanId;
		}
		public String getServiceClass()
		{
			return serviceClass;
		}
		public void setServiceClass(String serviceClass)
		{
			this.serviceClass = serviceClass;
		}
		public String getAddress()
		{
			return address;
		}
		public void setAddress(String address)
		{
			this.address = address;
		}
		
	}

}
